//
//  HomeViewController.m
//  weibo
//
//  Created by hp on 16/4/21.
//  Copyright © 2016年 hp. All rights reserved.
//

#import "HomeViewController.h"
#import "VisitorView.h"
#import "UIBarButtonItem+Create.h"
#import "TitleButton.h"
#import "PopoverViewController.h"
#import "PopoverAnimation.h"
#import "HomeViewController.h"
@interface HomeViewController ()

@property (nonatomic, strong) PopoverAnimation *popoverAnimation;
@property (nonatomic, strong) PopoverViewController *popVC;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (![[UserAccount sharedUserAccount] isLogined]) {
        [self.visitorView setupVisitorInfo:YES imageName:@"visitordiscover_feed_image_house" message:@"关注一些人，回这里看看他们的消息"];
        return;
    }
    
    [self setupNavi];
    // 监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:kNotificationPopoverWillShow object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:kNotificationPopoverWillDismiss object:nil];
    
    
}

- (void)change:(NSNotification *)n
{
    TitleButton *titleButton = (TitleButton *)self.navigationItem.titleView;
    titleButton.selected = !titleButton.selected;
}

- (void)dealloc
{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setupNavi
{
    // 创建左右按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem createBarButtonItem:@"navigationbar_friendattention" target:self action:@selector(leftItemBtnPressed:)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem createBarButtonItem:@"navigationbar_pop" target:self action:@selector(rightItemBtnPressed:)];
    // 2 创建标题按钮
    TitleButton *titleBtn = [[TitleButton alloc] init];
    [titleBtn setTitle:@"HP" forState:UIControlStateNormal];
    [titleBtn sizeToFit];
    [titleBtn addTarget:self action:@selector(titleBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = titleBtn;
}

- (void)titleBtnPressed:(UIButton *)sender
{
    // 弹出菜单
    UIStoryboard *popoverS = [UIStoryboard storyboardWithName:@"PopoverViewController" bundle:[NSBundle mainBundle]];
    PopoverViewController *popVC = [popoverS instantiateViewControllerWithIdentifier:@"popoverViewController"];
//    //采用自定义转场动画
//    popVC.transitioningDelegate = self.popoverAnimation;
//    self.popoverAnimation.popoverFrame = CGRectMake(self.view.bounds.size.width/2.0f-50,64, 100, 200);
//    // 自定义转场动画
//    popVC.modalPresentationStyle = UIModalPresentationCustom;
//
//    [self presentViewController:popVC animated:YES completion:nil];
    
    self.popVC = popVC;
//    [self.navigationController.view addSubview:self.popVC.view];
    [UIView animateWithDuration:0.5 animations:^{
        
        self.popVC.view.frame = CGRectMake(self.view.bounds.size.width/2.0f-50,64, 100, 200);
    }];
    // 蒙版
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2];
    bgView.frame = [UIScreen mainScreen].bounds;
    [bgView addSubview:self.popVC.view];
    // 将弹出的视图加到UITabBarController上面
    [self.tabBarController.view addSubview:bgView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDo:)];
    [bgView addGestureRecognizer:tap];
    
}

- (void)tapDo:(UITapGestureRecognizer *)gesture
{
    [UIView animateWithDuration:0.5 animations:^{
        self.popVC.view.transform = CGAffineTransformMakeScale(1.0, 0.001);
    } completion:^(BOOL finished) {
        [self.popVC.view.superview removeFromSuperview];
    }];
}

- (void)leftItemBtnPressed:(id)sender
{
    
}

- (void)rightItemBtnPressed:(id)sender
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"QRCodeViewController" bundle:[NSBundle mainBundle]];
    // 此方法需要在storyboard中设置启动页面
    UINavigationController *navi = [sb instantiateInitialViewController];
    [self presentViewController:navi animated:YES completion:nil];
}

//- (PopoverAnimation *)popoverAnimation
//{
//    if (_popoverAnimation==nil) {
//        _popoverAnimation = [[PopoverAnimation alloc] init];
//    }
//    return _popoverAnimation;
//}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    
    
    
    return cell;
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
